<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline" :mdl="mdl">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="角色名称">
              <a-input placeholder="请输入" v-model="mdl.name"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" @click="select">查询</a-button>
              <a-button style="margin-left: 8px" @click="mdl.name=''">重置</a-button>
            </span>
          </a-col>
          <a-col :md="8" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" icon="plus" @click="add">新建</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <s-table
      ref="table"
      size="default"
      :columns="columns"
      :data="loadData"
    >
      <div
        slot="expandedRowRender"
        slot-scope="record"
        style="margin: 0">
        <a-row
          :gutter="24"
          :style="{ marginBottom: '12px' }">
          <a-col :span="12" v-for="(role, index) in record.permissions" :key="index" :style="{ marginBottom: '12px' }">
            <a-col :lg="4" :md="24">
              <span>{{ role.permissionName }}：</span>
            </a-col>
            <a-col :lg="20" :md="24" v-if="role.actionEntitySet.length > 0">
              <a-tag color="cyan" v-for="(action, k) in role.actionEntitySet" :key="k">{{ action.describe }}</a-tag>
            </a-col>
            <a-col :span="20" v-else>-</a-col>
          </a-col>
        </a-row>
      </div>
      <span slot="action" slot-scope="text, record">
        <a @click="edit(record)">编辑</a>
        <a-divider type="vertical" />
        <a-dropdown>
          <a class="ant-dropdown-link">
            更多 <a-icon type="down" />
          </a>
          <a-menu slot="overlay">
            <a-menu-item>
              <a href="javascript:;">详情</a>
            </a-menu-item>
            <a-menu-item>
              <a href="javascript:;">禁用</a>
            </a-menu-item>
            <a-menu-item>
              <a @click="del(record)">删除</a>
            </a-menu-item>
            <a-menu-item>
              <a @click="handlerAuthor(record)">分配权限</a>
            </a-menu-item>
          </a-menu>
        </a-dropdown>
      </span>
    </s-table>
    <create-form ref="createModal" @ok="handleOk" />
    <update-form ref="updateModal" @ok1="handleOk1" />
    <a-modal
     title="权限分配"
     :visible="visibleAuthor"
     :confirm-loading="confirmLoadingAuthor"
     @ok="handleOkAuthor"
     @cancel="handleCancelAuthor"
     width="800px"
    >
     <group-authority ref="groupAuthority" :groupId="groupId" ></group-authority>
    </a-modal>
  </a-card>
</template>

<script>
import { STable } from '@/components'
import { getRoleList, delRole, addRole, getRoleById, updateRole, getCompany, assignPermissions } from '@/api/user'
import CreateForm from './modiles/CreateForm'
import UpdateForm from './modiles/UpdateForm'
import GroupAuthority from '../group/modules/GroupAuthority.vue'

export default {
  name: 'Role',
  components: {
    STable,
    CreateForm,
    UpdateForm,
    GroupAuthority
  },
  data () {
    return {
      description: '列表使用场景：后台管理中的权限管理以及角色管理，可用于基于 RBAC 设计的角色权限控制，颗粒度细到每一个操作类型。',
      refresh: true,
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      form: null,
      mdl: {
        name: ''
      },
      // 高级搜索 展开/关闭
      advanced: false,
      // 表头
      columns: [
        {
          title: '角色id',
          dataIndex: 'roleId'
        },
        {
          title: '用户名称',
          dataIndex: 'name'
        },
        {
          title: '角色描述',
          dataIndex: 'description',
          sorter: true
        },
        {
          title: '创建时间',
          dataIndex: 'ctime',
          sorter: true
        },
        {
          title: '角色所属单位ID',
          dataIndex: 'roleUnit',
          sorter: true
        },
        {
          title: '角色所属单位',
          dataIndex: 'roleUnitName',
          sorter: true
        }, {
          title: '操作',
          width: '150px',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 查询参数
      queryParam: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return getRoleList(this.mdl.name)
        .then(res => {
            console.log('getRoleList', res)
            return res.result
          })
      },

      selectedRowKeys: [],
      selectedRows: [],
      visibleAuthor: false,
      dialogAuthorityName: '关联资源',
      groupId: -1,
      confirmLoadingAuthor: false
    }
  },
  created () {
    getRoleList()
    .then(res => console.log('getRoleList', res))
  },
  methods: {
    select () {
      const th = this
      setTimeout(() => {
        th.$refs.table.refresh()
      }, 1000)
    },
    add () {
      getCompany().then(resp => {
        this.$refs.createModal.jg = resp.data
      })
      this.$refs.createModal.add()
    },
    edit (record) {
      getCompany().then(resp => {
        this.$refs.updateModal.jg = resp.data
      })
      getRoleById(record.roleId).then(resp => {
        this.$refs.updateModal.mdl.roleId = resp.info.roleId
        this.$refs.updateModal.mdl.name = resp.info.name
        this.$refs.updateModal.mdl.description = resp.info.description
        this.$refs.updateModal.mdl.roleunit = resp.info.roleunit
      })
      this.$refs.updateModal.edit(record)
    },
    del (record) {
      delRole(record.roleId).then(res => {
        console.log('delRole.call()', res)
      })
      this.select()
    },
    handlerAuthor (record) {
     this.visibleAuthor = true;
     debugger
     this.$refs.groupAuthority.roleId.roleId = record.roleId
     this.$refs.groupAuthority.upmsUserRoleDepPageRequest.roleId = record.roleId
     this.$refs.groupAuthority.fetchs()
    },
    handleOkAuthor (e) {
      // this.ModalText = 'The modal will be closed after two seconds';
      // this.confirmLoadingAuthor = true;
      debugger
      this.$message.info('menu checked> ' + this.$refs.groupAuthority.checkedKeys.checked)
      this.$message.info('menu checkedKeys---> ' + this.$refs.groupAuthority.checkedKeys)
      this.$message.info('menu selectedKeys--> ' + this.$refs.groupAuthority.selectedKeys)
      this.$message.info('table selectedRowKeys-->' + this.$refs.groupAuthority.selectedRowKeys)
     /* setTimeout(() => {
        this.visibleAuthor = false;
        this.confirmLoadingAuthor = false;
      }, 2000); */
    },
    handleCancelAuthor (e) {
      console.log('Clicked cancel button');
      this.visibleAuthor = false;
    },
    assignPermissions (record) {
      assignPermissions(record.roleId).then(res => {
        console.log('assignPermissions.call()', res)
      })
      this.select()
    },
    closeAuthorityDialog () {
      this.dialogAuthorityVisible = false;
    },
    handleOk (values) {
      addRole(values)
      this.select()
    },
    handleOk1 (values) {
      updateRole(values)
      this.select()
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    }
  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  }
}
</script>
